package com.hdsx.validator.validate.processor;

import java.lang.reflect.Field;

import org.apache.ibatis.ognl.Ognl;

import com.esri.arcgis.geometry.IGeometry;
import com.hdsx.validator.util.GeometryUtil;
import com.hdsx.validator.validate.annotation.JTS;
import com.hdsx.validator.validate.log.ILog;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.WKBReader;

public class GeometryProcessor extends AbstractGeometryProcessor implements IProcessor {

	@Override
	public void process(Object obj, Field field, ILog log) throws Exception {
		// TODO Auto-generated method stub
		JTS ognl=field.getAnnotation(JTS.class);
		if(ognl!=null)
		{
			String sql=ognl.sql();
			String layerName=ognl.layerName();
			String message=ognl.message();
			String sqlBuilder="";
			try
			{
				String [] sqls=sql.split("#");
				for(String ss:sqls)
				{
					int index=ss.indexOf("{")+1;
					int last=ss.indexOf("}");
					if(index!=-1&&last!=-1)
					{
						String ff=ss.substring(index, last);
						sqlBuilder+="'"+Ognl.getValue(ff, obj)+"'"+ss.substring(ss.lastIndexOf("}")+1, ss.length());
					}
					else
					{
						sqlBuilder=ss;
					}
				}
			}catch(Exception e){
				throw new Exception("sql参数异常{"+sql+"}");
			}
			IGeometry shape=(IGeometry) Ognl.getValue("shape", obj);
			byte[] bts=GeometryUtil.convertGeometryToWKB(shape);
			WKBReader reader=new WKBReader();
			Geometry GeoB=reader.read(bts);
			int relation=ognl.spatial().getSpatial();
			if(!doPosts(sqlBuilder,GeoB,relation,layerName))
			{
				log.record(message);
			}
		}
	}

	
}
